package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.config.annotation.RequiresPermissions;
import com.zsr.example.entity.SysLog;
import com.zsr.example.entity.SysOperationLog;
import com.zsr.example.service.SysLogService;
import com.zsr.example.service.SysOperationLogService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.vo.SysLogReq;
import com.zsr.example.vo.SysOptLogReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsr
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/operation-log")
public class SysOperationLogController {
    @Autowired
    private SysOperationLogService operationLogService;

    @RequiresPermissions("log:list")
    @PostMapping("pageListOptLogCondition/{current}/{limit}")
    public JSONObject pageListLogCondition(
            @PathVariable long current,
            @PathVariable long limit,
            @RequestBody(required = false) SysOptLogReq sysOptLogReq) {//@RequestBody
        // 使用json封装到参数,需要post请求
        //创建page对象
        Page<SysOperationLog> page = new Page<>(current, limit);

        operationLogService.pageQueryView(page, sysOptLogReq);

        long total = page.getTotal();//总记录数
        List<SysOperationLog> records = page.getRecords(); //数据list集合

        return CommonUtil.successPage(total, records);
    }


    @RequiresPermissions("log:delete")
    @DeleteMapping("{id}")
    public JSONObject removeTeacher(
            @PathVariable String id) {
        boolean flag = operationLogService.removeById(id);
        return flag ? CommonUtil.successJson() : CommonUtil.errorJson();
    }
}

